package com.ubxtech.db.config;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import com.ubxtech.core.generator.IdGenerator;
import com.ubxtech.core.generator.impl.HuToolIdGenerator;
import com.ubxtech.core.generator.impl.NoneIdGenerator;
import com.ubxtech.db.handler.CustomMybatisPlusInterceptor;
import com.ubxtech.db.handler.TenantEqValueHandlerImpl;
import com.ubxtech.db.handler.TenantMultiValueHandlerImpl;
import com.ubxtech.db.handler.TenantMultiValueInterceptor;
import com.ubxtech.db.properties.DbProperties;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import jakarta.annotation.Resource;

/**
 * @author Victor.Xiao
 * @since 2022-01-07 10:17
 **/
@Configuration
@EnableConfigurationProperties(DbProperties.class)
@MapperScan("com.ubxtech.**.mapper")
@Slf4j
public class MybatisPlusConfig {

    @Resource
    private DbProperties dbProperties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = DbProperties.PREFIX, name = "id-type", havingValue = "HU_TOOL",matchIfMissing = true)
    public IdGenerator getHuToolIdGenerator() {
        if (dbProperties.getWorkId() != null && dbProperties.getCenterId() != null) {
            return new HuToolIdGenerator(dbProperties.getWorkId(), dbProperties.getCenterId());
        }
        return new HuToolIdGenerator();
    }


    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = DbProperties.PREFIX, name = "id-type", havingValue = "NONE")
    public IdGenerator getNoneIdGenerator() {
        return new NoneIdGenerator();
    }


    @Bean
    @ConditionalOnMissingBean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new CustomMybatisPlusInterceptor();
        if (StrUtil.equals(dbProperties.getTenantType(), "column")) {
            interceptor.addInnerInterceptor(new TenantMultiValueInterceptor(new TenantMultiValueHandlerImpl(dbProperties)));
        }
        //乐观锁插件
        if (dbProperties.getOptimisticLocker() != null && dbProperties.getOptimisticLocker() == 1) {
            interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        }

        // 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
        // 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
        // 分页插件
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        // 单页分页条数限制
        paginationInterceptor.setMaxLimit(dbProperties.getMaxLimit());
        paginationInterceptor.setDbType(DbType.MYSQL);
        // 溢出总页数后是否进行处理
        paginationInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInterceptor);
        //防止全表更新与删除插件
        if (dbProperties.getIsBlockAttack()) {
            interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        }
        // sql性能规范插件
        if (dbProperties.getIsIllegalSql()) {
            interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
        }
        return interceptor;
    }

}
